<?php
/* --------------------------------------------------------------
   PropertiesView.php 2012-02-09 gambio
   Gambio GmbH
   http://www.gambio.de
   Copyright (c) 2009 Gambio GmbH
   Released under the GNU General Public License (Version 2)
   [http://www.gnu.org/licenses/gpl-2.0.html]
   --------------------------------------------------------------
*/

#TODO: create language files
define('PROPERTIES_TEXT_COMBI_NOT_AVAILABLE', 'Diese Kombination ist leider nicht lieferbar..');
define('PROPERTIES_TEXT_COMBI_OUT_OF_STOCK', 'Diese Kombination ist in dieser Anzahl aktuell leider nicht lieferbar.');
define('PROPERTIES_TEXT_COMBI_OK', 'Verf&uuml;gbar.');

require_once(DIR_FS_CATALOG . 'gm/inc/gm_prepare_number.inc.php');

  function steves_json_encode($a=false)
  {
    if (is_null($a)) return 'null';
    if ($a === false) return 'false';
    if ($a === true) return 'true';
    if (is_scalar($a))
    {
      if (is_float($a))
      {
        // Always use "." for floats.
        return floatval(str_replace(",", ".", strval($a)));
      }

      if (is_string($a))
      {
        static $jsonReplaces = array(array("\\", "/", "\n", "\t", "\r", "\b", "\f", '"'), array('\\\\', '\\/', '\\n', '\\t', '\\r', '\\b', '\\f', '\"'));
        return '"' . str_replace($jsonReplaces[0], $jsonReplaces[1], $a) . '"';
      }
      else
        return $a;
    }
    $isList = true;
    for ($i = 0, reset($a); $i < count($a); $i++, next($a))
    {
      if (key($a) !== $i)
      {
        $isList = false;
        break;
      }
    }

	$coo_json = new GMJSON(false);

    $result = array();
    if ($isList)
    {
      foreach ($a as $v) $result[] = $coo_json->encode($v);
      return '[' . join(',', $result) . ']';
    }
    else
    {
      foreach ($a as $k => $v) $result[] = $coo_json->encode($k).':'.$coo_json->encode($v);
      return '{' . join(',', $result) . '}';
    }
  }


class PropertiesView 
{
	var $v_coo_properties_control = false;
	
	var $m_env_get_array = array();
	var $m_env_post_array = array();
	
	var $m_content_array = array();
	var $m_content_template = '';
	
	function PropertiesView($p_get_array=false, $p_post_array=false) 
	{
		$this->v_coo_properties_control = MainFactory::create_object('PropertiesControl');
		
		if($p_get_array) $this->m_env_get_array = $p_get_array;
		if($p_post_array) $this->m_env_post_array = $p_post_array;
	}
	
	
	function proceed()
	{
		$t_output = '';
		return $t_output;
	}
	
	function get_order_details_by_combis_id($p_properties_combis_id, $p_order_details_type)
	{
		$t_language_id = $_SESSION['languages_id'];
		$t_properties_data_array = $this->v_coo_properties_control->get_properties_combis_details($p_properties_combis_id, $t_language_id);
								
		$t_html_output = $this->build_order_details($t_properties_data_array, $p_order_details_type);
		return $t_html_output;
	}
	
	function build_order_details($p_properties_data_array, $p_order_details_type)
	{
		$t_content_data_array = array(
									'PROPERTIES_DATA' => $p_properties_data_array
								);
		
		switch($p_order_details_type)
		{
			case 'mail_html':
				$t_content_template = 'order_details_mail.html';
				break;
			
			case 'mail_text':
				$t_content_template = 'order_details_mail.txt';
				break;
			
			case 'cart':
			default:
				$t_content_template = 'order_details_cart.html';
				break;
		}
		
		$t_html_output = $this->build_html($t_content_data_array, $t_content_template);
		return $t_html_output;
	}
	
	# -1 not available
	#  0 out of stock
	#  1 available
	function get_combis_status_code($p_products_id, $p_value_ids_array, $p_need_qty=1)
	{
		$t_return_code = 0; # return value
		
		$t_products_properties_combis_id = $this->v_coo_properties_control->get_combis_id_by_value_ids_array($p_products_id, $p_value_ids_array);
		if($t_products_properties_combis_id == 0) 
		{
			# not found
			$t_return_code = -1; 
		}
		else {
			# found -> check quantity
			$t_return_code = $this->get_combis_status_code_by_combis_id($p_products_id, $t_products_properties_combis_id, $p_need_qty);
		}
		return $t_return_code;
	}

	#  0 out of stock
	#  1 available
	function get_combis_status_code_by_combis_id($p_products_id, $p_combis_id, $p_need_qty=1)
	{
		$t_return_code = 0; # return value
        
        // get quantity check
        $coo_products = MainFactory::create_object('GMDataObject', array('products', array('products_id' => $p_products_id) ));
        $use_properties_combis_quantity = $coo_products->get_data_value('use_properties_combis_quantity');
        
        if($use_properties_combis_quantity == 1){
            $t_quantity = $coo_products->get_data_value('products_quantity');
        }else{
            $t_quantity = $this->v_coo_properties_control->get_properties_combis_quantity($p_combis_id);
        }
        
		if($t_quantity < $p_need_qty)
		{
			# out of stock
			$t_return_code = 0;
		}
		else {
			# combi found and available
			$t_return_code = 1;
		}
		return $t_return_code;
	}

	function get_combis_status_code_text($p_status_code)
	{	
		switch($p_status_code)
		{
                        case -1: 	 	 
                                $t_output_text = PROPERTIES_TEXT_COMBI_NOT_AVAILABLE; 	 	 
                                break; 
			case 0:
				$t_output_text = PROPERTIES_TEXT_COMBI_OUT_OF_STOCK;
				break;
			case 1:
				$t_output_text = PROPERTIES_TEXT_COMBI_OK;
				break;
		}
		return $t_output_text;
	}

	function get_combis_status_json($p_products_id, $p_value_ids_array, $p_need_qty=1)
	{
		$t_output_json = false;

		# get CODE and TEXT
		$t_status_code = $this->get_combis_status_code($p_products_id, $p_value_ids_array, $p_need_qty);
		$t_status_text = $this->get_combis_status_code_text($t_status_code);
                
        $coo_products = MainFactory::create_object('GMDataObject', array('products', array('products_id' => $p_products_id) ));

        $use_properties_combis_quantity = $coo_products->get_data_value('use_properties_combis_quantity');

        if($t_status_code != -1) # "!= -1" = combination possible  
        { 
            if(($use_properties_combis_quantity == 0 && (ATTRIBUTE_STOCK_CHECK == 'false'|| STOCK_CHECK == 'false')) || $use_properties_combis_quantity == 3 || STOCK_ALLOW_CHECKOUT == 'true')
            {
                    $t_status_code = 1; # combi ok
                    $t_status_text = '';
            }
        }

		$t_status_array = array(
							'STATUS_CODE' => $t_status_code,
							'STATUS_TEXT' => $t_status_text
							);
							
		$t_output_json = steves_json_encode($t_status_array);
		return $t_output_json;
	}
	
	function get_combis_status_by_combis_id_json($p_combis_id, $p_need_qty=1)
	{
		$t_output_json = false;

        $coo_products_properties_combis = MainFactory::create_object('GMDataObject', array('products_properties_combis', array('products_properties_combis_id' => $p_combis_id) ));
        
        $coo_products = MainFactory::create_object('GMDataObject', array('products', array('products_id' => $coo_products_properties_combis->get_data_value('products_id')) ));
        $use_properties_combis_quantity = $coo_products->get_data_value('use_properties_combis_quantity');
        
		# get CODE and TEXT
		$t_status_code = $this->get_combis_status_code_by_combis_id($coo_products_properties_combis->get_data_value('products_id'), $p_combis_id, $p_need_qty);
		$t_status_text = $this->get_combis_status_code_text($t_status_code);
           
        if($t_status_code != -1) # "!= -1" = combination possible  
        { 
            if(($use_properties_combis_quantity == 0 && (ATTRIBUTE_STOCK_CHECK == 'false'|| STOCK_CHECK == 'false')) || $use_properties_combis_quantity == 3 || STOCK_ALLOW_CHECKOUT == 'true')
            {
                    $t_status_code = 1; # combi ok
                    $t_status_text = '';
            }
        }

		$t_status_array = array(
							'STATUS_CODE' => $t_status_code,
							'STATUS_TEXT' => $t_status_text
							);

		$t_output_json = steves_json_encode($t_status_array);
		return $t_output_json;
	}
        
        function get_combis_exists($p_products_id, $p_value_ids_array, $p_need_qty=1)
	{
		$t_return_code = 0; # return value
		
		$t_products_properties_combis_id = $this->v_coo_properties_control->get_combis_id_by_value_ids_array($p_products_id, $p_value_ids_array);
		if($t_products_properties_combis_id == 0) 
		{
			# not found
			$t_return_code = 0; 
		}
		else {
			# found -> return $t_products_properties_combis_id
			$t_return_code = $t_products_properties_combis_id;
		}
		return $t_return_code;
	}

	function get_selection_form($p_products_id, $p_language_id, $p_selected_ids_array=false)
	{
		$c_products_id = (int)$p_products_id;
		$c_language_id = (int)$p_language_id;
		
		$t_html_output = '';
		
		$t_selection_form_type = 'dropdowns';

		$t_properties_combis_array = $this->v_coo_properties_control->get_products_properties_combis_data($c_products_id, $c_language_id);

		switch($t_selection_form_type)
		{
			case 'dropdowns':
				$coo_properties_combis_admin_control = MainFactory::create_object('PropertiesCombisAdminControl');
				$t_properties_dropdown_mode = $coo_properties_combis_admin_control->get_properties_dropdown_mode($c_products_id);
				$t_properties_price_show = $coo_properties_combis_admin_control->get_properties_price_show($c_products_id);
				$t_use_properties_combis_weight = $coo_properties_combis_admin_control->get_use_properties_combis_weight($c_products_id);
				$t_use_properties_combis_quantity = $coo_properties_combis_admin_control->get_use_properties_combis_quantity($c_products_id);
				$t_use_properties_combis_shipping_time = $coo_properties_combis_admin_control->get_use_properties_combis_shipping_time($c_products_id);
				
				$t_properties_array = $this->v_coo_properties_control->get_products_properties_data($c_products_id, $c_language_id);
				$t_content_data_array = array(
											'products_id'				=> $c_products_id,
											'properties_dropdown_mode'	=> $t_properties_dropdown_mode,
											'properties_price_show'		=> $t_properties_price_show,
											'use_properties_combis_weight'	=> $t_use_properties_combis_weight,
											'use_properties_combis_quantity'=> $t_use_properties_combis_quantity,
											'use_properties_combis_shipping_time'=> $t_use_properties_combis_shipping_time,
											'properties_currency'		=> $_SESSION['currency'],
											'PROPERTIES_DATA'			=> $t_properties_array
										);
				$t_content_template = 'selection_forms/dropdowns.html';
				break;
			
			case 'combis_table':
			default:
				$t_content_data_array = array(
											'products_id'   		 => $c_products_id,
											'PROPERTIES_COMBIS_DATA' => $t_properties_combis_array
										);
				$t_content_template = 'selection_forms/combis_table.html';
				break;
		}
				
		// prepare combis weight
		$xtPrice = new xtcPrice($_SESSION['currency'], $_SESSION['customers_status']['customers_status_id']);
		foreach($t_properties_combis_array AS $key => $value){
			
			if(APPEND_PROPERTIES_MODEL == "true") {
				$t_properties_combis_array[$key]['append_products_model'] = "true";
			}else{
				$t_properties_combis_array[$key]['append_products_model'] = "false";
			}
			
                        if($t_use_properties_combis_weight == 1){
                            $t_properties_combis_array[$key]['combi_weight'] = gm_prepare_number($t_properties_combis_array[$key]['combi_weight'], $xtPrice->currencies[$xtPrice->actualCurr]['decimal_point']);
                        }else{
                            $coo_products = MainFactory::create_object('GMDataObject', array('products', array('products_id' => $c_products_id)));
                            $products_weight = $coo_products->get_data_value('products_weight');
                            $t_properties_combis_array[$key]['combi_weight'] = gm_prepare_number($t_properties_combis_array[$key]['combi_weight']+$products_weight, $xtPrice->currencies[$xtPrice->actualCurr]['decimal_point']);                            
                        }
		}

		$coo_json = new GMJSON(false);
		$t_properties_combis_json = $coo_json->encode($t_properties_combis_array);
                
		$t_content_data_array = array_merge(
									$t_content_data_array,
									array(
										'PROPERTIES_COMBIS_JSON' => $t_properties_combis_json,
										'ATTRIBUTE_STOCK_CHECK' => ATTRIBUTE_STOCK_CHECK
									)
								);

		//echo '<pre>'.$t_properties_combis_json.'</pre>';

		$t_html_output = $this->build_html($t_content_data_array, $t_content_template);
		return $t_html_output;
	}
		
	
	
	
	# standard function
	function build_html($p_content_data_array, $p_template_file, $p_add_languages=true)
	{
		# language array for assigning in smarty template
		if($p_add_languages)
		{
			$t_languages_array = $this->v_coo_properties_control->get_shop_languages_data();
			$p_content_data_array = array_merge(
										array('LANGUAGES' => $t_languages_array), 
										$p_content_data_array);
		}
		$coo_smarty = new Smarty();
		
		# assign content
		$coo_smarty->assign('content_data', $p_content_data_array);
		
		# compile settings
		$coo_smarty->compile_dir = DIR_FS_CATALOG.'templates_c/';
		$coo_smarty->caching = false;
		
		# get html content
		$t_full_template_path = DIR_FS_CATALOG.'templates/'.CURRENT_TEMPLATE.'/module/properties/'.$p_template_file;
		//$coo_smarty->clear_compiled_tpl($t_full_template_path);
		//$coo_smarty->clear_compiled_tpl();
		
		$t_html_output = $coo_smarty->fetch($t_full_template_path);
		return $t_html_output;
	}
	
	# ???
	function get_output() 
	{
		$t_content_array = $this->m_content_array;
		$t_template_file = $this->m_content_template;
		
		$t_html_output = $this->build_html($t_content_array, $t_template_file); 
		return $t_html_output;
	}
}















?>